class UsersController < ApplicationController
  before_filter :require_admin, :only => [:destroy, :admin]
  before_filter :require_user, :only => [:edit, :update]
  before_filter :require_no_user, :only => [:new, :create]
  before_filter :init, :only => [:new, :create, :activate, :forget, :recover, :reset, :edit, :update]
  include ControllerHelper
  def admin
    per_page = params[:per_page] ||= @site["per_page"]["admin"]["users"]
    conditions = merge_conditions(conditions, ["approved = ?", 't']) if params[:status] == "approved"
    conditions = merge_conditions(conditions, ["approved <> ?", 't']) if params[:status] == "banned"
    @users = User.find(:all, :conditions => conditions).paginate(:page => params[:page], :per_page => per_page)
    set_admin_settings
    render :layout => "cms"
  end

  def show
    @user = User.find(:first, :conditions => ["id = ?", params[:id]])
    if @user.nil?
      flash[:notice] = I18n.t "site.message.no_user"
      redirect_to root and return
    end
    @page["name"] = "userprofile"
    @page["bonus_title"] = @page["bonus_description"] = @user.nickname
    @page["bonus_header"] = @user.nickname
    render :layout => "profile"
  end

  def new
    @page["styles"] << "roundmenuUI"
    @page["meta"] = false
    @page["name"] = "reg"
    @user = User.new
  end

  def create
    @user = User.new params[:user]
    @user.active = false
    @user.approved = true
    @user.code = random_string 20
    @user.set_roles("user")
    @page["meta"] = false
#    if verify_recaptcha && @user.save TODO: uncomment
    if @user.save
      Notifier.after_reg(@user, activate_url(:login => @user.login, :code => @user.code), root_url, @site["site"]["email"]).deliver
      @page["name"] = "reg.thanks"
    else
      @page["styles"] << "roundmenuUI"
      @page["name"] = "reg"
      render :action => "new"
    end
  end

  def activate
    @user = User.find(:first, :conditions => ["login = ? AND code = ?", params[:login], params[:code]])
    if @user.nil?
      @page["success"] = false
    else
      @user.active = true
      @user.code = nil
      @user.save
    end
    @page["name"] = "reg.activation"
    @page["meta"] = false
  end

  def forget
    @page["styles"] << "roundmenuUI"
    @page["meta"] = false
    @page["name"] = "forget"
    @user = User.new
  end

  def recover
    @user = User.find(:first, :conditions => ["email = ?", params[:user][:email]])
    unless @user.nil?
      @user.code = random_string 20
#      if verify_recaptcha && @user.save
      if @user.save
        Notifier.recover(@user, reset_url(:login => @user.login, :code => @user.code), root_url, @site["site"]["email"]).deliver
        @page["name"] = "forget.recover"
        @page["meta"] = false
      else
        @page["styles"] << "roundmenuUI"
        @page["meta"] = false
        @page["name"] = "forget"
        render :action => "forget"
      end
    else
      @page["name"] = "forget.recover"
      @page["meta"] = false
      @page["success"] = false
    end
  end

  def reset
    user = User.find(:first, :conditions => ["login = ? AND code = ?", params[:login], params[:code]])
    if user.nil?
      @page["success"] = false
    else
      user.code = nil
      new_password = random_string 10
      user.password = new_password
      user.password_confirmation = new_password
      Notifier.new_password(user, root_url, @site["site"]["email"]).deliver
      user.save
      current_user_session.destroy if current_user_session
    end
    @page["meta"] = false
    @page["name"] = "forget.reset"
  end

  def edit
    @page["styles"] << "roundmenuUI"
    @page["meta"] = false
    @page["name"] = "edit_user"
    @page["not_show"] = "update"
    @user = User.find(:first, :conditions => ["id = ?", params[:id]])
    redirect_to edit_user_path(:id => current_user.id) and return if @user.nil?
    redirect_to edit_user_path(:id => current_user.id) if @user.id != current_user.id && !@current_roles.include?("admin")
  end

  def update
    @user = User.find(:first, :conditions => ["id = ?", params[:id]])
    redirect_to edit_user_path(:id => current_user.id) and return if @user.nil?
    if @current_roles.include?("admin")
      if @user.update_attributes(params[:user])
        redirect_to user_admin_path(:status => "all")
      else
        @page["styles"] << "roundmenuUI"
        @page["meta"] = false
        @page["name"] = "edit_user"
        render :action => "edit", :id => current_user.id
      end
    else
      @user.firstname = params[:user][:firstname]
      @user.lastname = params[:user][:lastname]
      @user.city = params[:user][:city]
      @user.address = params[:user][:address]
      @user.phone = params[:user][:phone]
      @user.mobile = params[:user][:mobile]
      @user.password = params[:user][:password] unless params[:user][:password].empty?
      @user.password = params[:user][:password_confirmation] unless params[:user][:password_confirmation].empty?
      if @user.save
        @page["name"] = "edit_user.thanks"
        @page["meta"] = false
      else
        @page["styles"] << "roundmenuUI"
        @page["meta"] = false
        @page["name"] = "edit_user"
        render :action => "edit", :id => current_user.id
      end
    end
  end

  def destroy
    @user = User.find params[:id]
    @user.destroy
    redirect_to user_admin_path(:status => "all")
  end

  def approve_ban
    user = User.find(params[:id])
    old_status = user.approved ? "approved" : "banned"
    user.approved = !user.approved
    user.save
    redirect_to user_admin_path(:status => old_status)
  end

end
